package com.zyl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.pojo.*;
import com.zyl.pojo.Article;
import com.zyl.service.*;
import com.zyl.util.ResultVoUtil;
import com.zyl.vo.*;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@RestController
@RequestMapping("/admin")
public class Admin {
    @Autowired
    CommentRecordService commentRecordService;
    @Autowired
    ArticleService articleService;
    @Autowired
    FriendService friendService;
    @Autowired
    PrivateWordService privateWordService;
    @Autowired
    UpdateService updateService;
    @Autowired
    CategoryService categoryService;
    @PostMapping("/comment")
    public ResultVo commentRecord(@RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        List<AdminRecordVo> list = commentRecordService.findAllComment();
        PageInfo<AdminRecordVo> Comment = new PageInfo<AdminRecordVo>(list);
        Comment.setTotal(commentRecordService.commentCount());
        CommentVo commentVo=new CommentVo(Comment);
        return ResultVoUtil.success(commentVo);
    }

    @GetMapping("/deleteComment/{commentId}")
    public ResultVo deleteCommentById(@PathVariable("commentId") Integer commentId){
            commentRecordService.deleteCommentById(commentId);
            return ResultVoUtil.success(null);
    }

    @PostMapping("/article")
    public ResultVo articleRecord(@RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        ArticleVo list = articleService.findAll();
        List<ArticleVo> articleVos=new ArrayList<>();
        articleVos.add(list);
        PageInfo<ArticleVo> articleVo=new PageInfo<ArticleVo>(articleVos);
        articleVo.setTotal(articleService.articleCount());
        return ResultVoUtil.success(articleVo);
    }

    @GetMapping("/article/{articleId}")
    public ResultVo articleToUpdate(@PathVariable("articleId") Long articleId){
        AdminArticleVo articleVo = articleService.findArticleByArticleId(articleId);
        return ResultVoUtil.success(articleVo);
    }

    @PostMapping("/article/update")
    public ResultVo articleUpdate(@RequestBody AdminArticleVo adminArticleVo){
        Article article=new Article();
        article.setArticleId(adminArticleVo.getArticleId());
        article.setArticleCategories(adminArticleVo.getCategory());
        article.setArticleContent(adminArticleVo.getContext());
        article.setArticleTabloid(adminArticleVo.getDesc());
        article.setArticleTitle(adminArticleVo.getTitle());
        article.setUpdateDate(adminArticleVo.getUpdateDate());
        articleService.updateArticleByArticleId(article);
        return ResultVoUtil.success(null);
    }
    @PostMapping("/article/addArticle")
    public ResultVo articleAdd(@RequestBody AdminArticleVo adminArticleVo){
        articleService.insertArticle(adminArticleVo);
        List<CategoryVo> categoryByArticleName = categoryService.findCategoryByArticleName(adminArticleVo.getCategory());
        if (categoryByArticleName==null){
        categoryService.addCategory(adminArticleVo.getCategory());
        }
        return ResultVoUtil.success(null);
    }
    @GetMapping("/deleteArticle/{articleId}")
    public ResultVo deleteArticleById(@PathVariable Long articleId){
        articleService.deleteArticleByArticleId(articleId);
        return ResultVoUtil.success(null);
    }
    @PostMapping("/friendLink")
    public ResultVo findFriendLink(@RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        List<FriendLink> list = friendService.findAll();
        PageInfo<FriendLink> all=new PageInfo<>(list);
        all.setTotal(friendService.friendLinkCount());
        return ResultVoUtil.success(all);
    }
    @GetMapping("/friendLink/{id}")
    public ResultVo toUpdateFriendLink(@PathVariable Integer id){
        FriendLink friendLink = friendService.findFriendLinkById(id);
        return ResultVoUtil.success(friendLink);
    }
    @PostMapping("/friendLink/update")
    public ResultVo updateFriendLink(@RequestBody FriendLink friendLink){
        friendService.updateFriendLink(friendLink);
        return ResultVoUtil.success(friendLink);
    }
    @PostMapping("/addFriendLink")
    public ResultVo addFriendLink(@RequestBody FriendLink friendLink){
        friendService.insertFriendLink(friendLink);
        return ResultVoUtil.success(null);
    }
    @GetMapping("/deleteFriendLink/{id}")
    public ResultVo deleteFriendLink(@PathVariable Integer id){
        friendService.deleteFriendLinkById(id);
        return ResultVoUtil.success(null);
    }
    @PostMapping("/privateword")
    public ResultVo privateWord(@RequestBody Page page){
        PageHelper.offsetPage((page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
        List<PrivateWord> list = privateWordService.findAll();
        PageInfo<PrivateWord> all=new PageInfo<>(list);
        all.setTotal(privateWordService.privateWordCount());
        return ResultVoUtil.success(all);
    }
    @GetMapping("/privateword/reply/{id}")
    public ResultVo PrivateWordResponse(@PathVariable("id") Integer id){
        PrivateWord privateWordById = privateWordService.findPrivateWordById(id);
        return ResultVoUtil.success(privateWordById);
    }

    @PostMapping("/privateWord/response")
    public ResultVo ResponsePrivateWord(@RequestBody PrivateWord privateWord){
            privateWord.setReplierId("0");
            privateWordService.responsePrivateWord(privateWord);
            return ResultVoUtil.success(null);
    }
    @GetMapping("/update")
    public ResultVo updateAll(){
        List<Update> all = updateService.findAll();
        return ResultVoUtil.success(all);
    }
    @GetMapping("/update/delete/{id}")
    public ResultVo deleteUpdate(@PathVariable("id") Integer id){
            updateService.deleteUpdateById(id);
            return ResultVoUtil.success(null);
    }
   @PostMapping("/addUpdate")
    public ResultVo addUpdate(@RequestBody Update update){

            updateService.addUpdate(update);
            return ResultVoUtil.success(null);
    }
}
